package com.sairui.Xiaoruilib.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import cn.iyivi.R;


/**
 * Created by sunqiyong on 2017/9/9.
 */

public class WXTouchView extends View {
    private Paint mLinePaint;
    private Paint mSolidPaint;
    private Paint mTextPaint;
    private int colorWhite;
    private int colorSolid;
    private int colorLine;
    private int colorText;

    private Boolean isTouch = false;

    private String content = "";

    private Bitmap mBackGround;//中间的图片

    float mDownY = 0;//按下时手指的y坐标值
    boolean isCancel = false;//是否已经取消发送

    private int moreX = 0;
    private int moreY = 0;

    private TouchListener mTouchListener;

    public WXTouchView(Context context) {
        super(context);
    }

    public void setTouchListener(TouchListener mTouchListener) {
        this.mTouchListener = mTouchListener;
    }

    public WXTouchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        colorWhite = ContextCompat.getColor(context, R.color.white);
        colorSolid = ContextCompat.getColor(context, R.color.main_line_color);
        colorLine = ContextCompat.getColor(context, R.color.time_color);
        colorText = ContextCompat.getColor(context, R.color.main_black);

        mLinePaint = new Paint();
        mLinePaint.setStrokeWidth(2);
        mLinePaint.setStyle(Paint.Style.STROKE);

        mSolidPaint = new Paint();
        mSolidPaint.setColor(colorWhite);
        mLinePaint.setStyle(Paint.Style.FILL);

        mTextPaint = new Paint();
        mTextPaint.setColor(colorText);
        mTextPaint.setStrokeWidth(0);
        mTextPaint.setTextSize(42);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mTextPaint.setStrokeCap(Paint.Cap.ROUND);//设置笔帽

        mBackGround = BitmapFactory.decodeResource(getResources(),R.mipmap.ico_speak);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isTouch) {
            mLinePaint.setColor(colorSolid);
            mSolidPaint.setColor(colorSolid);
            content = "松开 结束";
        } else {
            mSolidPaint.setColor(colorWhite);
            mLinePaint.setColor(colorLine);
            content = "按住 说话";
        }

        @SuppressLint("DrawAllocation")
        RectF r1 = new RectF(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(r1, 20, 20, mLinePaint);
        @SuppressLint("DrawAllocation")
        RectF r2 = new RectF(2, 2, getWidth() - 2, getHeight() - 2);
        canvas.drawRoundRect(r2, 20, 20, mSolidPaint);

        float middleX = getWidth() / 2;
        float middleY = getHeight() / 2;
        float picStartX = middleX - (mBackGround.getWidth() * 3 / 2) - mTextPaint.measureText(content) / 2 ;
        float picStartY = middleY - (mBackGround.getHeight() / 2);
        canvas.drawBitmap(mBackGround, picStartX, picStartY, mSolidPaint);

        Paint.FontMetricsInt fm = mTextPaint.getFontMetricsInt();
        canvas.drawText(content, getWidth() / 2 - mTextPaint.measureText(content) / 2, getHeight() / 2 + (fm.bottom - fm.top) / 2 - fm.bottom, mTextPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mTouchListener == null) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchListener.start_recognize();
                mDownY = event.getY();
                isTouch = true;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                if (isCancel) {
                    break;
                }
                //上划取消
                if (mDownY - event.getY() > getHeight()) {
                    mTouchListener.cancel_recognize();
                    isCancel = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isCancel) {
                    isCancel = false;
                } else {
                    mTouchListener.end_recognize();
                }
                mTouchListener.actionUp();
                isTouch = false;
                invalidate();
                break;
        }
        return true;
    }

    public interface TouchListener {
        void start_recognize();//开始识别

        void cancel_recognize();//上滑取消

        void end_recognize();//手指抬起，结束识别，获取内容

        void actionUp();
    }

}
